package com.hainingtong.user.shiro.realm;


import com.hainingtong.user.shiro.token.PhonePasswordToken;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.mindrot.jbcrypt.BCrypt;

public class PhonePasswordRealm extends BaseRealm {

    /**
     * 自定义匹配密码的方法
     */
    public PhonePasswordRealm() {
        this.setCredentialsMatcher(new CredentialsMatcher() {
            @Override
            public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {
                return BCrypt.checkpw(authenticationToken.getCredentials().toString(), authenticationInfo.getCredentials().toString());
            }
        });
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof PhonePasswordToken;
    }
}
